package com.q.tqb.wxapi;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.q.tqb.base.AppConst;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //  setContentView(R.layout.pay_result);
        api = WXAPIFactory.createWXAPI(this, AppConst.WEIXIN_APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            if (resp.errCode == 0) {
                Intent intent = new Intent();
                if (AppConst.ORDER_TYPE == 1) {
                    intent.setAction(AppConst.PAY_SPEAKING);
                    Toast.makeText(this, "支付成功，发送红包成功", Toast.LENGTH_SHORT).show();
                } else if (AppConst.ORDER_TYPE == 2) {
                    intent.setAction(AppConst.PAY_IMAGE);
                    Toast.makeText(this, "支付成功，发送红包成功", Toast.LENGTH_SHORT).show();
                } else if (AppConst.ORDER_TYPE == 3) {
                    intent.setAction(AppConst.PAY_MAP);
                    Toast.makeText(this, "支付成功，发送红包成功", Toast.LENGTH_SHORT).show();
                } else if (AppConst.ORDER_TYPE == 4) {
                    intent.setAction(AppConst.PAY_TWEETER);
                    Toast.makeText(this, "支付成功，升级推客成功", Toast.LENGTH_SHORT).show();
                } else if (AppConst.ORDER_TYPE == 5) {
                    intent.setAction(AppConst.WX_XJB);
                    Toast.makeText(this, "现金币购买成功", Toast.LENGTH_SHORT).show();
                }
                sendBroadcast(intent);

            } else {
                if (AppConst.ORDER_TYPE == 4) {
                    Toast.makeText(this, "支付失败，升级推客失败", Toast.LENGTH_SHORT).show();
                } else if (AppConst.ORDER_TYPE == 5) {
                    Toast.makeText(this, "现金币购买失败", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, "支付失败，发送红包失败", Toast.LENGTH_SHORT).show();
                }
            }
            finish();

        }
    }
}